package com.joinway.framework.core.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import com.joinway.framework.bean.exception.SessionExpiredException;
import com.joinway.framework.core.plugin.PluginManager;
import com.joinway.framework.core.service.IWebExceptionService;

public abstract class WebExceptionController {

	private final static Logger log = LoggerFactory.getLogger(WebExceptionController.class);
	
	@Autowired List<IWebExceptionService> services;
	
    @ExceptionHandler(Exception.class)
    public ModelAndView handleInternalException(Exception e, HttpServletRequest request) throws Exception {
        log.warn("internal error", e);
        
        return getHandlerService().getErrorPage();
    }

    @ExceptionHandler(SessionExpiredException.class)
    public ModelAndView handleSessionExpiredException(SessionExpiredException e, HttpServletRequest request) throws Exception {
        log.warn("internal error", e);
        
        return getHandlerService().getIndexPage();
    }

	protected IWebExceptionService getHandlerService() {
		return PluginManager.getInstance(services);
	}
}
